﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SharePointLogBrowser2
{
    public partial class Filters : UserControl
    {
        public Filters()
        {
            InitializeComponent();
        }

        public void SetFilters()
        {
            AddItems(clbProcess, LogItemContext.Current.FilterItemsByProcess());
            AddItems(clbArea, LogItemContext.Current.FilterItemsByArea());
            AddItems(clbCategory, LogItemContext.Current.FilterItemsByCategory());
            AddItems(clbLevel, LogItemContext.Current.FilterItemsByLevel());
        }

        private static void AddItems(CheckedListBox clb, FilterItemCollection filterItems)
        {
            clb.Items.Clear();
            foreach (FilterItem filterItem in filterItems.OrderBy(c => c.Text))
            {
                clb.Items.Add(filterItem, filterItem.Selected);
            }
        }

        internal FilterItemDictionary GetFilterItemByProcess()
        {
            return GetFilterItems(clbProcess);
        }
        internal FilterItemDictionary GetFilterItemByArea()
        {
            return GetFilterItems(clbArea);
        }
        internal FilterItemDictionary GetFilterItemByCategory()
        {
            return GetFilterItems(clbCategory);
        }
        internal FilterItemDictionary GetFilterItemByLevel()
        {
            return GetFilterItems(clbLevel);
        }

        private FilterItemDictionary GetFilterItems(CheckedListBox clb)
        {
            FilterItemDictionary items = new FilterItemDictionary();

            for(int i =0; i< clb.Items.Count; i++)
            {
                FilterItem item = clb.Items[i] as FilterItem;
                if (item != null)
                {
                    item.Selected = clb.GetItemChecked(i);
                    if (item.Selected)
                    {
                        items.Add(item.Text, item);
                    }
                }
            }

            return items;
        }

        private void clbProcessEnabler_CheckedChanged(object sender, EventArgs e)
        {

            DisEnableCheckListBoxItems(clbProcess, clbProcessEnabler);
        }

        private void DisEnableCheckListBoxItems(CheckedListBox clb, CheckBox clbEnabler)
        {
            for (int i = 0; i < clb.Items.Count; i++)
            {
                clb.SetItemChecked(i, clbEnabler.Checked);
            }
        }

        private void clbCategoryEnabler_CheckedChanged(object sender, EventArgs e)
        {
            DisEnableCheckListBoxItems(clbCategory, clbCategoryEnabler);
        }

        private void clbAreaEnabler_CheckedChanged(object sender, EventArgs e)
        {
            DisEnableCheckListBoxItems(clbArea, clbAreaEnabler);
        }

        private void clbLevelEnabler_CheckedChanged(object sender, EventArgs e)
        {
            DisEnableCheckListBoxItems(clbLevel, clbLevelEnabler);
        }
    }
}
